package zjc.bookkeeping.homepage.addpage;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import zjc.bookkeeping.R;
import zjc.bookkeeping.adapter.ConsumptionClassAdapter;
import zjc.bookkeeping.db.ConsumptionBean;
import zjc.bookkeeping.db.ConsumptionClassBean;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.db.MethodBean;
import zjc.bookkeeping.server.StaticData;
import zjc.bookkeeping.tools.BusinessDialog;
import zjc.bookkeeping.tools.KeyboardDialog;
import zjc.bookkeeping.tools.MethodDialog;
import zjc.bookkeeping.tools.RemarksDialog;
import zjc.bookkeeping.tools.TimeDialog;
import zjc.bookkeeping.tools.ToolsClass;


public class OutcomeFragment extends Fragment implements View.OnClickListener {
    ImageView consumptionClassIv;
    TextView consumptionClassTv,moneyTv,remarksTv,timeTv,methodTv,businessTv;
    GridView consumptionClassGv;
    Button sureBtn;
    List<ConsumptionClassBean> consumptionClassBeanList;
    ConsumptionClassAdapter adapter;
    ConsumptionBean consumptionBean;
    MethodBean methodBean;
    String method = StaticData.getMethodName();
    float money;
    int id;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        consumptionBean = new ConsumptionBean();
//        写入默认数据
        consumptionBean.setConsumption_class_name("娱乐");
        consumptionBean.setConsumption_remarks("无备注");
        consumptionBean.setConsumption_money(0);
        consumptionBean.setaccount_id(DBManager.selectAccountId()+"");
        consumptionBean.setConsumption_method("无账户");
        consumptionBean.setConsumption_business("无商家");
        consumptionBean.setConsumption_kind(0);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_outcome, container, false);
        initView(view);
        setNowTime();
        loadDataToGv();
        setGvListener();
        return view;
    }
    //    读取并写入现在系统时间
    private void setNowTime() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String time = simpleDateFormat.format(date);
        timeTv.setText(time);
        consumptionBean.setConsumption_time(time);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        consumptionBean.setConsumption_year(year);
        consumptionBean.setConsumption_month(month);
        consumptionBean.setConsumption_day(day);
    }

    //    分类图标点击事件并写入分类信息
    private void setGvListener() {
        consumptionClassGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                adapter.selectPos = i;
                adapter.notifyDataSetInvalidated();
                ConsumptionClassBean consumptionClassBean = consumptionClassBeanList.get(i);

                String consumptionClassName = consumptionClassBean.getConsumption_class_name();
                String consumptionClassImage = consumptionClassBean.getConsumption_class_image();

                consumptionClassIv.setImageResource(ToolsClass.getResourceId(consumptionClassImage));
                consumptionClassTv.setText(consumptionClassName);

                consumptionBean.setConsumption_class_name(consumptionClassName);
            }
        });
    }

    //    填充消费分类
    private void loadDataToGv() {
        consumptionClassBeanList = new ArrayList<>();
        List<ConsumptionClassBean> outcomeList = DBManager.getConsumptionClass(0);
        consumptionClassBeanList.addAll(outcomeList);
        adapter = new ConsumptionClassAdapter(getContext(), consumptionClassBeanList);
        consumptionClassGv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        consumptionClassTv.setText("娱乐");
        consumptionClassIv.setImageResource(R.drawable.entertainment);
    }

    //    加载控件
    private void initView(View view) {
        remarksTv = view.findViewById(R.id.outcome_tv_remarks);
        businessTv = view.findViewById(R.id.outcome_tv_business);
        consumptionClassIv = view.findViewById(R.id.outcome_iv_consumption_class);
        moneyTv = view.findViewById(R.id.outcome_tv_money);
        consumptionClassTv = view.findViewById(R.id.outcome_tv_consumption_class);
        methodTv = view.findViewById(R.id.outcome_tv_method);
        if (method != null){
            methodTv.setText(method);
            consumptionBean.setConsumption_method(method);
        }
        timeTv = view.findViewById(R.id.outcome_tv_time);

        consumptionClassGv = view.findViewById(R.id.outcome_gv_class);

        sureBtn = view.findViewById(R.id.outcome_btn_sure);

        moneyTv.setOnClickListener(this);
        remarksTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);
        methodTv.setOnClickListener(this);
        businessTv.setOnClickListener(this);
        sureBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.outcome_tv_money:
                showMoneyDialog();
                break;
            case R.id.outcome_tv_remarks:
                showRemarksDialog();
                break;
            case R.id.outcome_tv_time:
                showTimeDialog();
                break;
            case R.id.outcome_tv_method:
                getMethod();
                break;
            case R.id.outcome_tv_business:
                getBusiness();
                break;
            case R.id.outcome_btn_sure:
                if (consumptionBean.getConsumption_money() == 0){
                    ToolsClass.showToast(getContext(),"请输入金额，且不能为0.",Toast.LENGTH_SHORT);
                }else {
                    DBManager.insertConsumption(consumptionBean);
                    DBManager.updateMethodOutmonry(money,id);
                    getActivity().finish();
                }
                break;
        }
    }

    private void showRemarksDialog() {
        RemarksDialog remarksDialog = new RemarksDialog(getContext());
        remarksDialog.show();
        String str = remarksTv.getText().toString();
        remarksDialog.setRemarksEt(str);
        remarksDialog.setRemarksDialogSize();
        remarksDialog.setOnRemarksSureListener(new RemarksDialog.OnRemarksSureListener() {
            @Override
            public void onRemarksSure() {
                String str = remarksDialog.getRemarksEt();
                if (!TextUtils.isEmpty(str)){
                    remarksTv.setText(str);
                    consumptionBean.setConsumption_remarks(str);
                }
            }
        });
    }

    private void getBusiness() {
        BusinessDialog businessDialog = new BusinessDialog(getContext());
        businessDialog.show();
        businessDialog.setOnBusinessItemListener(new BusinessDialog.OnBusinessItemListener() {
            @Override
            public void onBusinessItemClick() {
                String str = businessDialog.getBusiness();
                businessTv.setText(str);
                consumptionBean.setConsumption_business(str);
            }
        });
    }

    private void getMethod() {
        MethodDialog methodDialog = new MethodDialog(getContext());
        methodDialog.show();
        methodDialog.setOnMethodItemListener(new MethodDialog.OnMethodItemListener() {
            @Override
            public void onMethodItemClick() {
                String str = methodDialog.getMethod();
                id = methodDialog.getMethodId();
                methodTv.setText(str);
                consumptionBean.setConsumption_method(str);
            }
        });
    }

    private void showTimeDialog() {
        TimeDialog timeDialog = new TimeDialog(getContext());
        timeDialog.show();
        timeDialog.setOnTimeSureListener(new TimeDialog.OnTimeSureListener() {
            @Override
            public void onTimeSure(String time, int year, int month, int day) {
                timeTv.setText(time);
                consumptionBean.setConsumption_time(time);
                consumptionBean.setConsumption_year(year);
                consumptionBean.setConsumption_month(month);
                consumptionBean.setConsumption_day(day);
            }
        });
    }


    private void showMoneyDialog() {
        KeyboardDialog keyboardDialog = new KeyboardDialog(getContext());
        keyboardDialog.show();
        keyboardDialog.setKeyboardDialogSize();
        keyboardDialog.setOnKeyboardSureListener(new KeyboardDialog.OnKeyboardSureListener() {
            @Override
            public void onKeyboardSure() {
                String str = keyboardDialog.getMoneyTv();
                if (!TextUtils.isEmpty(str)){
                    moneyTv.setText(str);
                    money = Float.parseFloat(str);
                    consumptionBean.setConsumption_money(money);
                }
            }
        });
    }
}